//
//  SEGOrderTasksView.m
//  Uhomebk
//
//  Created by mac on 2017/10/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "SEGOrderTasksView.h"
#define kVlineViewX 69.5
#define kVlineViewWidth 1
@implementation SEGOrderTasksView

-(instancetype)initWithFrame:(CGRect)frame taskModel:(SEGOrderTasks *)taskModel
{
    if (self = [super initWithFrame:frame]) {
        [self setupUIWithFrame:frame taskModel:taskModel];
        
    }
    return self;
}

/**
 UI布局
  */
-(void)setupUIWithFrame:(CGRect)frame taskModel:(SEGOrderTasks *)taskModel
{
    UIView *trackTasksView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 0)];
    trackTasksView.backgroundColor = kColor_W;
    [self addSubview:trackTasksView];
    CGFloat taskViewHeight = 0;
    //2.1子任务节点标识
    /* 节点名字*/
    UILabel *taskNameIcon = [[UILabel alloc] initWithFrame:CGRectMake(kVlineViewX+15, 12, 33, 15)];
    taskNameIcon.font = kFont_11;
    //加急和督办红色标签
    if ([taskModel.taskCode isEqualToString:@"ACTION_URGENT"] || [taskModel.taskCode isEqualToString:@"ACTION_SUP"]) {
        [taskNameIcon setTextColor:kColor_W];
        taskNameIcon.backgroundColor = kColor_S2;
    }else{
        [taskNameIcon setTextColor:kColor_1];
        taskNameIcon.backgroundColor = kColor_bg;
    }
    taskNameIcon.layer.cornerRadius = 8;
    taskNameIcon.layer.masksToBounds = YES;
    taskNameIcon.textAlignment = NSTextAlignmentCenter;
    taskNameIcon.text = taskModel.taskName;
    /* 竖线*/
    UIView *vLineView = [[UIView alloc] initWithFrame:CGRectMake(kVlineViewX, 0, 1, 0)];
    vLineView.backgroundColor = kColor_L2;
    /* 节点*/
    UIImageView *taskIcon = [[UIImageView alloc] initWithFrame: CGRectMake(kVlineViewX-4.5, CGRectGetMidY(taskNameIcon.frame)-5, 10, 10)];
    taskIcon.image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_orderpoint_gray_small")];
    /* 任务时间*/
    UILabel *taskTimeLbl = [[UILabel alloc] initWithFrame:CGRectMake(5, CGRectGetMidY(taskIcon.frame)-7.5, 50, 15)];
    taskTimeLbl.text = [self getTimeAndDateStrWith:taskModel][1];
    taskTimeLbl.textAlignment = NSTextAlignmentRight;
    [taskTimeLbl setTextColor:kColor_3];
    taskTimeLbl.font = kFont_10;
    /* 任务日期*/
  
    UILabel *taskDateLbl = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(taskTimeLbl.frame)+2, 40, 10)];
    taskDateLbl.text =  [self getTimeAndDateStrWith:taskModel][0];
    taskDateLbl.textAlignment = NSTextAlignmentRight;
    [taskDateLbl setTextColor:kColor_3];
    taskDateLbl.font = kFont_10;
    /* 任务描述信息*/
    CGFloat taskDescWidth = self.frame.size.width-kVlineViewX-30;
    NSString *taskDescStr = [self configTaskDescInfoWith:taskModel][0];
    CGSize taskDescStrSize = [taskDescStr getAttributionHeightWithlineSpace:6 font:kFont_15 width:taskDescWidth];
    UILabel *taskDescLbl = [[UILabel alloc] initWithFrame:CGRectMake(kVlineViewX+15, CGRectGetMaxY(taskNameIcon.frame)+12, taskDescWidth, taskDescStrSize.height)];
    taskDescLbl.numberOfLines = 0;
    [taskDescLbl setTextColor:kColor_1];
    [taskDescLbl extendAttributedString:taskDescStr lineSpace:6 font:kFont_15];
    
    
    /*环节处理意见*/
    NSString *taskAdviseStr = [self configTaskDescInfoWith:taskModel][1];
    CGFloat taskAdviseWidth = self.frame.size.width-kVlineViewX-30;
    CGSize taskAdviseStrSize = [taskAdviseStr getAttributionHeightWithlineSpace:6 font:kFont_15 width:taskAdviseWidth];
    UILabel *taskAdviseLbl = [[UILabel alloc] initWithFrame:CGRectMake(kVlineViewX+15, CGRectGetMaxY(taskDescLbl.frame)+12, taskAdviseWidth, taskAdviseStrSize.height)];
    taskAdviseLbl.numberOfLines = 0;
    [taskAdviseLbl setTextColor:kColor_1];
    [taskAdviseLbl extendAttributedString:taskAdviseStr lineSpace:6 font:kFont_15];
    //没有意见信息直接隐藏
    if (taskAdviseStr.length == 0) {
        taskAdviseLbl.height = 0;
        taskAdviseLbl.y -= 12;
    }
    //重新适配高度
    trackTasksView.height = CGRectGetMaxY(taskAdviseLbl.frame);
    taskViewHeight = CGRectGetMaxY(taskAdviseLbl.frame);
    vLineView.height = taskViewHeight;
    self.height = taskViewHeight;
    
    [trackTasksView addSubview:vLineView];
    [trackTasksView addSubview:taskNameIcon];
    [trackTasksView addSubview:taskIcon];
    [trackTasksView addSubview:taskTimeLbl];
    [trackTasksView addSubview:taskDateLbl];
    [trackTasksView addSubview:taskDescLbl];
    [trackTasksView addSubview:taskAdviseLbl];
    
    //如果是超时会下发督办信息，需要做特殊处理 ，不显示任务信息视图
    if ([taskModel.taskCode isEqualToString:@"ACTION_SUP"]) {
        if ( taskModel.supinfo.supuserId.length == 0 || [taskModel.supinfo.supuserId isEqualToString:@"0"]) {
            trackTasksView.height = 0;
            self.height = 0;
            vLineView.height = 0;
        }
    }
}


-(NSArray *)getTimeAndDateStrWith:(SEGOrderTasks *)tasksModel
{
    //    taskCode：
    //    督办：ACTION_SUP
    //    转单：ACTION_FORWARD
    //    挂起：ACTION_HANG
    //    追记：ACTION_COMMENT
    //    加急：ACTION_URGENT
    NSString *timeStr = @"";
    NSString *dateStr = @"";
    //1.督办任务项
    if ([tasksModel.taskCode isEqualToString:@"ACTION_SUP"]) {
        dateStr  = [SEGUtils getTimeWithTimeStr:tasksModel.supinfo.suptime timeFormat:@"MM-dd"];
        timeStr = [SEGUtils getTimeWithTimeStr:tasksModel.supinfo.suptime timeFormat:@"HH:mm"];
           }

    //2.转单任务项
    else if ([tasksModel.taskCode isEqualToString:@"ACTION_FORWARD"]){
        dateStr  = [SEGUtils getTimeWithTimeStr:tasksModel.rotateinfo.createDate timeFormat:@"MM-dd"];
        timeStr = [SEGUtils getTimeWithTimeStr:tasksModel.rotateinfo.createDate timeFormat:@"HH:mm"];
    }

    //3.挂起任务项
    else if ([tasksModel.taskCode isEqualToString:@"ACTION_HANG"]){
        dateStr  = [SEGUtils getTimeWithTimeStr:tasksModel.hanginfo.hangtime timeFormat:@"MM-dd"];
        timeStr = [SEGUtils getTimeWithTimeStr:tasksModel.hanginfo.hangtime timeFormat:@"HH:mm"];

    }

    //4.追记任务项
    else if ([tasksModel.taskCode isEqualToString:@"ACTION_COMMENT"]){
        dateStr  = [SEGUtils getTimeWithTimeStr:tasksModel.comment.createTime timeFormat:@"MM-dd"];
        timeStr = [SEGUtils getTimeWithTimeStr:tasksModel.comment.createTime timeFormat:@"HH:mm"];

    }

    //5.加急任务项
    else if ([tasksModel.taskCode isEqualToString:@"ACTION_URGENT"]){
        timeStr = [SEGUtils getTimeWithTimeStr:tasksModel.urgentinfo.createTime timeFormat:@"HH:mm"];
        dateStr  = [SEGUtils getTimeWithTimeStr:tasksModel.urgentinfo.createTime timeFormat:@"MM-dd"];

    }
    //不识别的隐藏
    else{
        timeStr = @"";
        dateStr = @"";
    }
    
    return @[dateStr,timeStr];
}
/**
 获取处理意见和描述信息文案
 */
-(NSArray *)configTaskDescInfoWith:(SEGOrderTasks *)tasksModel
{
    NSString *taskStr = @"";
    NSString *contentStr = @"";
    //    taskCode：
    //    督办：ACTION_SUP
    //    转单：ACTION_FORWARD
    //    挂起：ACTION_HANG
    //    追记：ACTION_COMMENT
    //    加急：ACTION_URGENT
    //1.督办任务项
    if ([tasksModel.taskCode isEqualToString:@"ACTION_SUP"] && tasksModel.supinfo.supuserId.length > 0 &&  ![tasksModel.supinfo.supuserId isEqualToString:@"0"] ) {
        taskStr = [NSString stringWithFormat:@"%@督办了工单",tasksModel.supinfo.supuserName];
        contentStr = tasksModel.supinfo.supremark.length>0? [NSString stringWithFormat:@"督办意见：%@",tasksModel.supinfo.supremark]:@"";
    }
    
    //2.转单任务项
    else if ([tasksModel.taskCode isEqualToString:@"ACTION_FORWARD"]){
        taskStr = [NSString stringWithFormat:@"%@进行了转单",tasksModel.rotateinfo.requserName];
        contentStr = tasksModel.rotateinfo.remark.length>0? [NSString stringWithFormat:@"转单意见：%@",tasksModel.rotateinfo.remark]:@"";
    }
    
    //3.挂起任务项
    else if ([tasksModel.taskCode isEqualToString:@"ACTION_HANG"]){
        taskStr = [NSString stringWithFormat:@"%@挂起了工单",tasksModel.hanginfo.userName];
        contentStr = tasksModel.hanginfo.hangremark.length>0? [NSString stringWithFormat:@"挂起意见：%@",tasksModel.hanginfo.hangremark]:@"";
    }
    
    //4.追记任务项
    else if ([tasksModel.taskCode isEqualToString:@"ACTION_COMMENT"]){
        taskStr = [NSString stringWithFormat:@"%@追记了工单",tasksModel.comment.userName];
        contentStr = tasksModel.comment.discussion.length>0? [NSString stringWithFormat:@"追记内容：%@",tasksModel.comment.discussion]:@"";
    }
    
    //5.加急任务项
    else if ([tasksModel.taskCode isEqualToString:@"ACTION_URGENT"]){
        taskStr = [NSString stringWithFormat:@"%@加急了工单",tasksModel.urgentinfo.userName];
        contentStr = tasksModel.urgentinfo.discussion.length>0? [NSString stringWithFormat:@"加急内容：%@",tasksModel.urgentinfo.discussion]:@"";
    }
    //不识别的隐藏
    else{
        taskStr = @"";
        contentStr = @"";
    }
    
    return @[taskStr,contentStr];
    
}

@end
